//
//  LYCycleScrollView.m
//  礼物go
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LYCycleScrollView.h"
#import "MatchViewController.h"

@implementation LYCycleScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.pageNum = 1;
        self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollV.bounces = NO;
        self.scrollV.delegate = self;
        self.scrollV.showsHorizontalScrollIndicator = NO;
        self.scrollV.showsVerticalScrollIndicator = NO;
        self.scrollV.pagingEnabled = YES;
        [self addSubview:self.scrollV];
    }
    return self;
}

-(void)setImages:(NSMutableArray *)urlArray{
    NSString *first = [urlArray firstObject];
    NSString *last = [urlArray lastObject];
    [urlArray insertObject:last atIndex:0];
    [urlArray addObject:first];
    self.scrollV.contentSize = CGSizeMake(self.scrollV.frame.size.width * urlArray.count, 0);
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:urlArray];
    int i = 0;
    for (NSString *name in array) {
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollV.frame.size.width * i, 0, self.scrollV.frame.size.width, self.scrollV.frame.size.height)];
        [imageV sd_setImageWithURL:[NSURL URLWithString:name] placeholderImage:[UIImage imageNamed:@"picholder"]];
        imageV.tag = 10000 + i;
        [self.scrollV addSubview:imageV];
        
        imageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageV addGestureRecognizer:tap];
        i++;
    }
    [self.scrollV setContentOffset:CGPointMake(self.frame.size.width, 0)];
    [self createPageControl:urlArray];
    [self createTimer];
}

-(void)createPageControl:(NSMutableArray *)array{
  self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 60, self.scrollV.frame.size.height - 30, self.frame.size.width / 4, 20)];
        //self.pageControl.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:self.pageControl];
       self.pageControl.numberOfPages = array.count - 2;
       self.pageControl.alpha = 0.5;
        self.pageControl.layer.masksToBounds = YES;
        self.pageControl.layer.cornerRadius = 10;
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    self.pageControl.currentPage = self.scrollV.contentOffset.x / SCREENWIDTH - 1;

}

-(void)pageAction:(UIPageControl *)pageControl{
[self.scrollV setContentOffset:CGPointMake(SCREENWIDTH * (pageControl.currentPage + 1), 0) animated:YES];

}

//创建计时器
-(void)createTimer{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

-(void)timerAction{
    self.pageNum ++;
    self.pageControl.currentPage++;
    CGFloat offSet = self.scrollV.frame.size.width * self.pageNum;
    [self.scrollV setContentOffset:CGPointMake(offSet, 0) animated:YES];
    
    //当不是正常滚动,滚动到边缘就取消动画
    NSInteger number = self.scrollV.contentSize.width / self.scrollV.frame.size.width;
    if (offSet == self.scrollV.frame.size.width * (number - 1)) {
        self.pageNum = 1;
        self.pageControl.currentPage = 0;
        [self.scrollV setContentOffset:CGPointMake(0, 0)];
    }
}

-(void)stopTimer{
    if (self.myTimer) {
        if (self.myTimer.isValid) {
            [self.myTimer invalidate];
            self.myTimer = nil;
        }
    }
    
}

-(void)tapAction:(UITapGestureRecognizer *)tap{
  // NSLog(@"pageNumber = %ld",self.pageNum);
   // UIImageView *imageV = (UIImageView *)tap.view;
//    NSLog(@"imageView = %ld",imageV.tag);
    
     self.pushBlock(self.pageNum - 1);
    
    return;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopTimer];
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self createTimer];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGSize size = scrollView.contentSize;
    CGFloat width = scrollView.frame.size.width;
    self.pageNum = scrollView.contentOffset.x / width;
    if (scrollView.contentOffset.x == (size.width - width)) {
        self.pageNum = 1;
        scrollView.contentOffset = CGPointMake(width, 0);
    }
    if (scrollView.contentOffset.x == 0) {
        self.pageNum = size.width / width - 2;
        [scrollView setContentOffset:CGPointMake(size.width - 2 * width, 0)];
    }
    self.pageControl.currentPage = self.pageNum - 1;
}























/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
